package com.snifferx.util;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.web.filter.OncePerRequestFilter;

import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;

/**
 * Filter for verifying if the submitted Captcha fields are valid.
 * <p>
 * This filter also allows you to set a proxy if needed
 */
public class CaptchaVerifierFilter extends OncePerRequestFilter {

	private Boolean useProxy = false;
	private String proxyPort;
	private String proxyHost;
	private String failureUrl;
	private CaptchaCaptureFilter captchaCaptureFilter;
	private String privateKey;
	
	private boolean captchaPassed;

	// Inspired by log output:
	// AbstractAuthenticationProcessingFilter.java:unsuccessfulAuthentication:320)
	// Delegating to authentication failure
	// handlerorg.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler@15d4273
	private SimpleUrlAuthenticationFailureHandler failureHandler = new SimpleUrlAuthenticationFailureHandler();

	@Override
	public void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {

		if (captchaCaptureFilter.getRequest() != null && captchaCaptureFilter.getUserCaptchaResponse() != null) {
			captchaPassed = SimpleImageCaptchaServlet.validateResponse(captchaCaptureFilter.getRequest(),
					captchaCaptureFilter.getUserCaptchaResponse());

			if (!captchaPassed) {
				
				//failureHandler.setDefaultFailureUrl(failureUrl);
				//failureHandler.onAuthenticationFailure(req, res, new BadCredentialsException("Captcha invalid!"));
				//req.getSession().setAttribute("CaptchaException", "Captcha is broken!"); //session  maybe  need to  remove  opretation 
				req.setAttribute("CaptchaException", "Captcha is broken!");
	            resetCaptchaFields();
			}else{
				resetCaptchaFields();
			}
		}
		chain.doFilter(req, res);
	}

	public Boolean getUseProxy() {
		return useProxy;
	}

	public void setUseProxy(Boolean useProxy) {
		this.useProxy = useProxy;
	}

	public String getProxyPort() {
		return proxyPort;
	}

	public void setProxyPort(String proxyPort) {
		this.proxyPort = proxyPort;
	}

	public String getProxyHost() {
		return proxyHost;
	}

	public void setProxyHost(String proxyHost) {
		this.proxyHost = proxyHost;
	}

	public String getFailureUrl() {
		return failureUrl;
	}

	public void setFailureUrl(String failureUrl) {
		this.failureUrl = failureUrl;
	}

	public CaptchaCaptureFilter getCaptchaCaptureFilter() {
		return captchaCaptureFilter;
	}

	public void setCaptchaCaptureFilter(CaptchaCaptureFilter captchaCaptureFilter) {
		this.captchaCaptureFilter = captchaCaptureFilter;
	}

	public String getPrivateKey() {
		return privateKey;
	}

	public void setPrivateKey(String privateKey) {
		this.privateKey = privateKey;
	}
	
	public void resetCaptchaFields() {
	    captchaCaptureFilter.setUserCaptchaResponse(null);
	}

	public boolean isCaptchaPassed() {
		return captchaPassed;
	}

	public void setCaptchaPassed(boolean captchaPassed) {
		this.captchaPassed = captchaPassed;
	}

	public SimpleUrlAuthenticationFailureHandler getFailureHandler() {
		return failureHandler;
	}

	public void setFailureHandler(SimpleUrlAuthenticationFailureHandler failureHandler) {
		this.failureHandler = failureHandler;
	}
}
